package com.niit.agricstore.controller;

import com.github.pagehelper.PageInfo;
import com.niit.agricstore.entity.Notice;
import com.niit.agricstore.entity.Product;
import com.niit.agricstore.entity.Type;
import com.niit.agricstore.entity.vo.ProductVo;
import com.niit.agricstore.service.GoodsService;
import com.niit.agricstore.util.DateUtil;
import com.niit.agricstore.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    /**商品分类**/
    //基本信息
    @GetMapping("/selectGoods/all")
    public Result selectProductAndPictureByPageAll(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo pageInfo = goodsService.selectProductAndPictureByPageAll(pageNum, pageSize);
        if (pageInfo != null) {
            return Result.success("200", "分页显示所有商品成功", pageInfo);
        } else {
            return Result.error("202", "分页显示所有商品失败");
        }
    }

    //根据产品分类名查询
    @GetMapping("/selectGoods/type")
    public Result selectProductAndPictureByPage(@RequestParam Integer typeId,
                                                @RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize){
        PageInfo pageInfo= goodsService.selectProductAndPictureByType(typeId,pageNum,pageSize);
        if(pageInfo!=null){
            return Result.success("200","分页显示分类商品成功",pageInfo);
        }else {
            return Result.error("202", "分页显示分类商品失败");
        }
    }
    //展示推荐字段商品
    @GetMapping("/selectGoods/rec")
    public Result selectRecProductAndPictureByPage(@RequestParam Integer typeId,
                                                @RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize){
        PageInfo pageInfo= goodsService.selectProductAndPictureByType(typeId,pageNum,pageSize);
        if(pageInfo!=null){
            return Result.success("200","分页显示分类商品成功",pageInfo);
        }else {
            return Result.error("202", "分页显示分类商品失败");
        }
    }
    //根据产品分类ID查询

    //添加产品分类
    @PostMapping("/add_type")
    public Result insert(String type_create_time1,String type_update_time1,Type type) throws Exception {
        DateUtil dateUtil=new DateUtil();
        Date date1=dateUtil.formatString(type_create_time1,"yyyy-MM-dd HH:mm:ss");
        type.setTypeCreateTime(date1);
        Date date2=dateUtil.formatString(type_update_time1,"yyyy-MM-dd HH:mm:ss");
        type.setTypeUpdateTime(date2);
        int row = goodsService.insert(type);
        if (row>0){
            return Result.success("200","新增产品分类成功",type);
        }else {
            return Result.error("202","新增产品分类失败~~~");
        }
    }
    //编辑产品分类
    @PutMapping("/update_type")
    public Result updateTypeById(String type_create_time1,String type_update_time1,Type type) throws Exception {
        DateUtil dateUtil1=new DateUtil();
        Date date1=dateUtil1.formatString(type_create_time1,"yyyy-MM-dd HH:mm:ss");
        type.setTypeCreateTime(date1);
        Date date2=dateUtil1.formatString(type_update_time1,"yyyy-MM-dd HH:mm:ss");
        type.setTypeUpdateTime(date2);
        int row = goodsService.updateTypeById(type);
        if (row>0){
            return Result.success("200","修改产品分类成功",type);
        }else {
            return Result.error("202","修改产品分类失败~~~");
        }
    }
    //删除分类
    @DeleteMapping("delete_type")
    public Result deleteById(int typeId){
        int row = goodsService.deleteTypeById(typeId);
        if (row>0){
            return Result.success("200","删除产品分类成功",row);
        }else {
            return Result.error("202","删除产品分类失败~~~");
        }
    }
    /**单品管理**/
    //创建
    @PostMapping("add_product")
    public Result addGoods(@RequestBody Map<String, Object> requestData) {
        String productName = (String) requestData.get("productName");
        String productOrigin = (String) requestData.get("productOrigin");
        Integer productSale = (int) requestData.get("productSale");
        String productDescription = (String) requestData.get("productDescription");
        Integer productTotal = (int) requestData.get("productTotal");
        Integer productNowPrice=(int)requestData.get("productNowPrice");

        // 构造 Product 对象
        Product product = new Product();
        product.setProductName(productName);
        product.setProductSale(productSale);
        product.setProductOrigin(productOrigin);
        product.setProductTotal(productTotal);
        product.setProductNowPrice(productNowPrice);


        int row = goodsService.addGoods(product);
        if (row > 0) {
            return Result.success("200", "新增单品成功", product);
        } else {
            return Result.error("202", "新增单品失败~~~");
        }
    }
    //修改单品


    //上架和下架货品(根据Product表中的rec判断)
    @DeleteMapping("/delete_goods")
    public Result deleteGoodsId(Integer productId){
        int row = goodsService.deleteGoodsId(productId);
        if (row>0){
            return Result.success("200","上下架单品成功",row);
        }else {
            return Result.error("202","上下架单品失败~~~");
        }
    }
    //根据产品ID查询
    @GetMapping("/selectProduct/{productId}")
    public Result selectGoodsById(@PathVariable("productId") Integer productId){
        Product product= goodsService.selectGoodsById(productId);
        if(product!=null){
            return Result.success("200","根据产品ID查询成功",product);
        }else {
            return Result.error("202", "根据产品ID查询失败");
        }
    }

    @GetMapping("/detail")
    public Result selectGoodsDetailsById(@RequestParam("productId")Integer productId){
        ProductVo product= goodsService.selectProductAndPictureById(productId);
        if(product!=null){
            return Result.success("200","根据产品ID查询成功",product);
        }else {
            return Result.error("202", "根据产品ID查询失败");
        }
    }
    @DeleteMapping("/delByGoodId/{productId}")
    public Result<Void> delByGoodId(@PathVariable int productId) {
        int rowsAffected = goodsService.delByGoodId(productId);
        if (rowsAffected > 0) {
            return Result.success();
        } else {
            return Result.error("202","Notice not found");
        }
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody Product product ) {
        goodsService.updateById(product);
        return Result.success();
    }


}
